﻿using GrainManage.Data.Model;
using GrainManage.Entities.Price;
using GrainManage.Proxy;
using GrainManage.Web.Models;
using GrainManage.Web.Models.Price;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GrainManage.Web.Controllers
{
    public class PriceController : BaseController
    {
        public ActionResult SearchRecentPrice()
        {
            return View();
        }

        public ActionResult SearchPrice()
        {
            return View();
        }

        public ActionResult NewPrice()
        {
            return View();
        }

        public ActionResult EditPrice(int priceID)
        {
            var inputPara = new UpdatePriceModel();
            if (priceID > 0)
            {
                var input = new InputGetByID
                {
                    Authentication = CurrentAccount,
                    PriceID = priceID
                };
                var priceProxy = ObjectFactory.PriceProxy;
                var result = priceProxy.GetByID(input);
                if (result.Status.StatusCode == 1)
                {
                    inputPara.PriceID = priceID;
                    inputPara.Grain = result.Price.Grain;
                    inputPara.PriceType = result.Price.PriceType;
                    inputPara.UnitPrice = result.Price.UnitPrice;
                    inputPara.Remarks = result.Price.Remarks;
                }
            }
            return View(inputPara);
        }

        [HttpPost]
        public ActionResult GetPrice(PriceFilterModel input, PageModel page)
        {
            var inputPara = new InputSearchPrice
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                PriceFilter = new Data.Model.Filter.PriceFilter
                {
                    StartTime = input.StartTime,
                    EndTime = input.EndTime,
                    PriceType = Filter.MatchWhole(input.PriceType),
                    Grain = Filter.MatchWhole(input.Grain)
                }
            };
            var priceProxy = ObjectFactory.PriceProxy;
            var result = priceProxy.SearchPrice(inputPara);
            return PartialView("_PriceList", result);
        }

        [HttpPost]
        public ActionResult GetRecentPrice(string grain, string priceType, PageModel page)
        {
            var inputPara = new InputSearchRecentPrice
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                PriceFilter = new Data.Model.Filter.PriceFilter
                {
                    PriceType = Filter.MatchWhole(priceType),
                    Grain = Filter.MatchWhole(grain)
                }
            };
            var priceProxy = ObjectFactory.PriceProxy;
            var result = priceProxy.SearchRecentPrice(inputPara);
            return PartialView("_RecentPriceList", result);
        }

        [HttpPost]
        public ActionResult GetLatestPrice(string grain, string priceType)
        {
            var inputPara = new InputGetRecentPriceByGrain
            {
                Authentication = CurrentAccount,
                PriceType = priceType,
                Grain = grain
            };
            var priceProxy = ObjectFactory.PriceProxy;
            var result = priceProxy.GetRecentPriceByGrain(inputPara);
            if (result != null && result.Price != null)
            {
                return NewtonsoftJson(result.Price);
            }
            return NewtonsoftJson(new GrainManage.Data.Model.Table.PriceTable());
        }

        [HttpPost]
        public ActionResult Insert(AddPriceModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputInsert
            {
                Authentication = CurrentAccount,
                Price = new Data.Model.Table.PriceTable
                {
                    Grain = input.Grain,
                    PriceType = input.PriceType,
                    Remarks = input.Remarks,
                    UnitPrice = input.UnitPrice
                }
            };
            var priceProxy = ObjectFactory.PriceProxy;
            var result = priceProxy.Insert(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return RedirectToAction("EditPrice", new { priceID = result.PriceID });
        }

        [HttpPost]
        public ActionResult Update(UpdatePriceModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputUpdate
            {
                Authentication = CurrentAccount,
                Price = new Data.Model.Table.PriceTable
                {
                    PriceID = input.PriceID,
                    Grain = input.Grain,
                    PriceType = input.PriceType,
                    Remarks = input.Remarks,
                    UnitPrice = input.UnitPrice
                }
            };
            var priceProxy = ObjectFactory.PriceProxy;
            var result = priceProxy.Update(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return RedirectToAction("EditPrice", new { priceID = input.PriceID });
        }

        [HttpPost]
        public ActionResult Delete(int priceID)
        {
            var inputPara = new InputDelete
            {
                Authentication = CurrentAccount,
                PriceID = priceID
            };
            var priceProxy = ObjectFactory.PriceProxy;
            var result = priceProxy.Delete(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }
    }
}
